﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.officer;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_upgradeextra officer_upgradeextra(string primarycode)
        {
            string userCode = SangoManager.Authentication();

            officer_upgradeextra response = ReflectionManager.CreatePojo<officer_upgradeextra>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Officer officer = OfficerDao.getUserOfficer(userCode, primarycode);

            if (!OfficerDao.isUpgrading(officer))
            {
                response.Message = "武将不在训练";
                return response;
            }

            // 检查用户最大等级

            if (officer.Officerlevel >= profile.Userlevel)
            {
                response.Message = "武将等级无法超越总部等级";
                return response;
            }

            // 检查用户升级队列

            Entities.Usr_Queue upgradeQueue = UsrQueueDao.getRunningQueue(userCode, CtiQueueTypecodeCollection.OFFICER_UPGRADE, officer.Primarycode);

            if (upgradeQueue == null)
            {
                response.Message = "武将不在训练";
                return response;
            }

            // 突飞猛进

            Entities.Usr_Queue upgradeExtraQueue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.OFFICER_UPGRADE_EXTRA, userCode, OfficerFormular.default_upgrade_extra_timespan);
            if (upgradeExtraQueue == null)
            {
                response.Message = "冷却中";
                return response;
            }

            Entities.Usr_Building officerBuilding = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.TRAIN);

            long soulvalue = OfficerFormular.getUpgradeExtraCostSoulValue(officerBuilding.Buildinglevel);

            if (!UsrProfileDao.tryDecreaseSoulValue(profile, soulvalue))
            {
                response.Message = "军魂不足";
                return response;
            }

            double increaseExp = OfficerFormular.getUpgradeExtraExperienceValue(officerBuilding.Buildinglevel);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(upgradeExtraQueue);

                command.Update(profile);

                // 获取旧升级数据

                long currentDate = GlobalTimer.Instance.ToTimeSequence();
                long enhanceBreakDate = (long)upgradeQueue.Queueenddate;// 临时结算日期
                if (enhanceBreakDate >= currentDate)
                {
                    enhanceBreakDate = currentDate;
                }
                long timespan = (long)upgradeQueue.Queueenddate - currentDate;

                {
                    double increaseFormerExp = OfficerFormular.getUpgradingExperienceByBuilding(//
                                officerBuilding.Buildinglevel, //
                                officer.Upgradestartdate, //
                                enhanceBreakDate, //
                                officer.Upgradespeedtypecode);

                    OfficerFormular.upgrade(officer, increaseFormerExp, profile.Userlevel, officerBuilding.Buildinglevel);
                }

                // 重新开始训练

                OfficerFormular.upgrade(officer, increaseExp, profile.Userlevel, officerBuilding.Buildinglevel);

                OfficerDao.refreshUpgrade(officer, currentDate);

                command.Update(officer);

                // refresh upgrade queue

                UsrQueueDao.UsrQueueRestart(upgradeQueue, timespan);

                command.Update(upgradeQueue);

                command.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.officer = OfficerFacade.ToOfficer(officer);
            response.upgradeextraqueue = UsrQueueFacade.ToQueue(upgradeExtraQueue);
            response.upgradequeue = UsrQueueFacade.ToQueue(upgradeQueue);
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_upgradeextra : ISangoResponse
    {
        player_profile profile { get;set;}
        player_queue upgradequeue { get;set;}
        player_queue upgradeextraqueue { get;set;}
        player_officer officer { get;set;}
    }
}
